/*
 * Course:        COMP 380
 * Semester:      Summer 2012
 * Assignment:    Text To Speech
 * File Name:     share.java
 * Author:        James Paul Pan
 * 				  Kenneth Wakeland
 * 				  Peter Xaymountry
 * Group #:		  Group #3
 */

package com.comp380.texttospeech;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/*
 * This class will let the user advertise Speak Easy to their friends
 * through email, Twitter or Facebook.
 */
public class share extends Activity implements OnClickListener
{
	// Initialize the button variables.
	Button btnShareEmail, btnShareTweet, btnShareFacebook, 
	btnNavMain, btnNavSaved, btnNavShare, btnNavImport;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// Keep the screen orientation in portrait mode
    	super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		// Set the layout for this activity.
		setContentView(R.layout.share);
		
		// Pass the variables to correspond with the Email button.
		btnShareEmail = (Button) findViewById(R.id.btnShareEmail);
		
		// When the button is click it goes to the onClick method and take
		// the user to their email to send a message.
		btnShareEmail.setOnClickListener(this);
		
		// Pass the variables to correspond with the Twitter button.
		btnShareTweet = (Button) findViewById(R.id.btnShareTweet);
		
		// When the button is click it goes to the onClick method and take
		// the user to their Twitter tweet to tweet about Speak Easy.
		btnShareTweet.setOnClickListener(this);
		
		// Pass the variables to correspond with the Facebook button.
		btnShareFacebook = (Button) findViewById(R.id.btnShareFacebook);
		
		// When the button is click it goes to the onClick method and take
		// the user to their Facebook to post about Speak Easy.
		btnShareFacebook.setOnClickListener(this);
		
		// Pass the variables to correspond with the Main button.
		btnNavMain = (Button) findViewById(R.id.btnNavMain);
		
		// When the button is click it goes to the onClick method and take
		// the user back to the Main screen.
		btnNavMain.setOnClickListener(this);
		
		// Pass the variables to correspond with the Saved button.
		btnNavSaved = (Button) findViewById(R.id.btnNavSaved);
		
		// When the button is click it goes to the onClick method and take
		// the user back to the Saved screen.
		btnNavSaved.setOnClickListener(this);
		
		// Pass the variables to correspond with the Share button.
		btnNavShare = (Button) findViewById(R.id.btnNavShare);
		
		// When the button is click it goes to the onClick method and tell
		// the user they are already in the Share screen.
		btnNavShare.setOnClickListener(this);
		
		// Pass the variables to correspond with the Import button.
		btnNavImport = (Button) findViewById(R.id.btnNavImport);
		
		// When the button is click it goes to the onClick method and take
		// the user back to the Import screen.
		btnNavImport.setOnClickListener(this);
	}
	
	/*
	 * This method will check if a application is installed.
	 * 
	 * @param uri
	 * 	This will represent the name of the application and see if
	 * 	it is installed.
	 */
	private boolean appInstalledOrNot(String uri)
    {
		// Initialize the PackageManger
        PackageManager pm = getPackageManager();
        
        // Set the boolean that check if the app is install to false.
        boolean app_installed = false;
        
        try 
        {
        	// Check to see if the app is installed.
        	pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        	
        	// Set the variable to be true to represent the app is installed.
            app_installed = true;
        }
        
        catch (PackageManager.NameNotFoundException e) 
        {
        	// Set the variable to be false to represent the app is not
        	// installed on this phone.
            app_installed = false;
        }
        
        // Return if the app is installed or not.
        return app_installed;
}

	
	/*
	 * This method will handle what happen when the user click on a particular 
	 * button.
	 */
	public void onClick(View v) 
	{
		if(v.getId() == R.id.btnNavMain)
		{
			// Display a message that the user click on the Main button.
			Toast.makeText(getBaseContext(), "Going to Main screen", Toast.LENGTH_SHORT).show();
			
			// This will start the transition to the Main screen.
			startActivity(new Intent(this, Text_to_speechActivity.class));
		}
		
		else if(v.getId() == R.id.btnNavSaved)
		{
			// Display a message that the user click on the Saved button.
			Toast.makeText(getBaseContext(), "Going to Saved screen", Toast.LENGTH_SHORT).show();
			
			// This will start the transition to the Saved screen.
			startActivity(new Intent(this, SavedAudio.class));
		}
		
		else if(v.getId() == R.id.btnNavImport)
		{
			// Display a message that the user click on the Import button.
			Toast.makeText(getBaseContext(), "Going to Import screen", Toast.LENGTH_SHORT).show();
			
			// This will start the transition to the Import screen.
			startActivity(new Intent(this, importwindow.class));
		}
		
		else if(v.getId() == R.id.btnNavShare)
		{
			// Display a message that the user click on the Share button.
			Toast.makeText(getBaseContext(), "Your already in the Share screen", Toast.LENGTH_SHORT).show();
		}
		
		// Initialize the intent variable and set it to null.
		Intent intent = null;
		
		// Initialize the manager variable and see it to null.
		PackageManager manager = null;
		
		// Initialize a boolean variable and set it to false.
		boolean installed = false;
		
		// Initialize a string variable and add a String message to it.
		String adMessage = "This is Advertisement! Tell your friends about Speak Easy! =)";
		
		// This switch statement will handle what happen when the user select the email button,
		// the twitter button or the facebook button.
    	switch (v.getId()) 
    	{
    		case R.id.btnShareFacebook:
    											// This variable will represent if the Facebook
    											// application is installed.
    											installed = appInstalledOrNot("com.facebook.katana");
    											
    											// If the application is installed, it will take you to it
    											// and direct you to the post screen in that application.
    											if(installed)
    											{
    												// Pass the intent variable to represent the send action
    												// which will deliver some data to someone else.
    												Intent intent3 = new Intent("android.intent.category.SEND");
    												
    												// Add a message to the subject box in the email.
    												intent3.putExtra(Intent.EXTRA_TEXT, adMessage);
    												
    												// Launch the application to the post screen.
    												intent3.setClassName("com.facebook.katana", "com.facebook.katana.ShareLinkActivity");
    												
    												Toast.makeText(getBaseContext(), "Please post your own message about Speak Easy",Toast.LENGTH_LONG).show();
    												
    												// This will start the transition to their Facebook screen.
    												startActivity(intent3);
    												break;
    											}
    											
    											// If the Facebook application is not installed, it will
    											// take you to the Facebook website through their browser.
    											else 
    											{
    												// Pass the intent variable to display the data to the user.
    												// This mean that it will show what site the browser is going to.
    												intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
    												
    												// This will start the transition to their browser, which will take them
    												// to the Facebook website.
    												startActivity(intent);
    												break;
    											}
    	
    	    case R.id.btnShareTweet:
    	    									// This variable will represent if the Twitter application
    	    									// is installed or not.
    	    									installed = appInstalledOrNot("com.twitter.android");
    	    									
    	    									// If the Twitter application is installed, it will take you
    											// to it and direct you to the tweet screen in that application.
    	    									if(installed)
    	    									{
    	    										// This variable will store the function that will 
    	    										// take the user to the twitter send screen.
    	    										intent = new Intent(android.content.Intent.ACTION_SEND);
    	    										
    	    										// This set the type of data being added.
    	    										intent.setType("text/plain");
    	    										
    	    										// This add a message to the intent.
    	    										intent.putExtra(android.content.Intent.EXTRA_TEXT, adMessage);
    	    										
    	    										// Store the application information to the manager variable.
    	    										manager = v.getContext().getPackageManager();
    	    										
    	    										List<ResolveInfo> activityListT = manager.queryIntentActivities(intent, 0);
    	    										for (final ResolveInfo app : activityListT) 
    	    										{
    	    											if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) 
    	    											{
    	    												final ActivityInfo activity = app.activityInfo;
    	    												final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
    	    												intent.addCategory(Intent.CATEGORY_LAUNCHER);
    	    												intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    	    												intent.setComponent(name);
    	    												v.getContext().startActivity(intent);
    	    												break;
    	    											}
    	    										}
    	    										break;
    	    									}
    	    									
    	    									// If the Twitter application is not installed, it will
    											// take you to the Twitter website through their browser.
    	    									else 
    	    									{
    	    										// Pass the intent variable to display the data to the user.
    												// This mean that it will show what site the browser is going to.
    	    										intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.twitter.com"));
    	    										
    	    										// This will start the transition to their browser, which will take them
    	    										// to the Twitter website.
    	    										startActivity(intent);
    	    										break;
    	    									}
    		
    	    case R.id.btnShareEmail:
    	    									// This variable will store the function that will 
    											// take the user to the Email select screen.
    	    									intent = new Intent(Intent.ACTION_SEND);
    	    									
    	    									// This add a message to the body of the email.
    	    									intent.putExtra(Intent.EXTRA_TEXT, adMessage); 
    	    									
    	    									// This specify the type not the data, so it just return that
    	    									// type of data.
    	    									intent.setType("text/plain"); 
    	    									
    	    									// This will start the transition to their Email screen.
    	    									startActivity(intent);
    	    									break;
    }
   }
}
